package cn.mandifal.seller.controller;

import cn.mandifal.pojo.DpSeller;
import cn.mandifal.seller.service.SellerService;
import cn.mandifal.utils.PhoneFormatCheckUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 登陆者的 controller
 * @author ws
 */
@RestController
@RequestMapping("/sellerLoginController")
public class SellerLoginController {
    @Reference
    private SellerService sellerService;
    /**
     * 显示用户名
     * @return
     */
    @RequestMapping("/showLoginerName")
    public Map showLoginerName(){
        Map map=new HashMap();
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        map.put("loginerName",name);
        return map;
    }

    /**
     * 修改密码
     * @param map
     * @return
     */
    @RequestMapping("/updatePassword")
    public Result updatePassword(@RequestBody Map map){
        //得到用户名
        String username = (String) map.get("username");
        //得到输入的原密码，进行比较
        DpSeller seller = sellerService.findOneSeller(username);
        String oldPassword = (String) map.get("oldPassword");
        //得到 security 的密码对象
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        //判断
        if(seller != null){
            String password = seller.getPassword();
            if (! bCryptPasswordEncoder.matches(oldPassword,password)){
                return Result.failed("原密码输入错误");
            }
        }else{
            return Result.failed("请重试");
        }
        String newPassword = (String) map.get("newPassword");
        //加密
        String encode = bCryptPasswordEncoder.encode(newPassword);
        seller.setPassword(encode);
        sellerService.updateSellerInfo(seller);
        return Result.success("密码修改成功");
    }
    /**
     * 发送验证码
     * @param email 手机号
     * @return
     */
    @RequestMapping("/sendCode")
    public Result sendCode(String email){
        try {
            System.out.println(email);
            sellerService.createCode(email);
            return Result.success("验证码发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("验证码发送失败");
        }
    }

    /**
     * 忘记密码
     */
    @RequestMapping("/forgetPassword")
    public Result forgetPassword(@RequestBody Map map){
        String sellerId = (String) map.get("sellerId");
        String email = (String) map.get("email");
        String emailCode = (String) map.get("emailCode");
        String password = (String) map.get("password");
        //如果查不到此商家返回错误
        DpSeller seller = sellerService.findSeller(sellerId, email);
        if (seller==null){
            return Result.failed("商家与邮箱不匹配");
        }
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        //验证码匹配成功
        if (sellerService.checkCode(email, emailCode)){
            seller.setPassword(bCryptPasswordEncoder.encode(password));
            //更新商家密码
            sellerService.updateSellerInfo(seller);
            return Result.success("密码修改成功");
        }
        return Result.failed();
    }
}
